<?php
/**
 * User: 五哥派
 * Date: 2024/7/17
 * Tel(同WX): 17335592719
 */
declare (strict_types=1);

namespace app\api\service\user;


use app\api\model\user\User as UserModel;
use app\api\service\BaseService;

use app\common\lib\exception\BaseException;
use think\Cache;
use think\Request;

class User extends BaseService
{
    // 当前登录的会员信息
    private static $currentLoginUser;

    /**
     * 获取当前登录的用户信息 (快捷)
     * 可在api应用中的任意模块中调用此方法(controller model service)
     * 已登录情况下返回用户信息, 未登录返回false
     * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
     * @return false
     * @throws BaseException
     */
    public static function getCurrentLoginUser(bool $isForce = false)
    {
        $service = new static;

        if (empty(static::$currentLoginUser)) {
            static::$currentLoginUser = $service->getLoginUser();

            if (empty(static::$currentLoginUser)) {
                $isForce && throwError($service->getError(), config('status.not_logged'));
                return false;
            }
        }
        return static::$currentLoginUser;
    }

    public function edit($userId, $data){
        //$userId = static::getCurrentLoginUserId(true);
        /*if(!empty($nick_name)){
            $data['nick_name'] = $nick_name;
        }
        if(!empty($avatar_path)){
            $data['avatar'] = $avatar_path;
        }*/

        $mUserModel = new UserModel();
        return $mUserModel->updateById($userId, $data);

    }
    public function edit1($userId, $nick_name, $avatar_path){
        //$userId = static::getCurrentLoginUserId(true);
        if(!empty($nick_name)){
            $data['nick_name'] = $nick_name;
        }
        if(!empty($avatar_path)){
            $data['avatar'] = $avatar_path;
        }

        $mUserModel = new UserModel();
        return $mUserModel->updateById($userId, $data);

    }
    /**
     * 获取当前登录的用户ID
     * getCurrentLoginUser方法的二次封装
     * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
     * @return int|false
     * @throws BaseException
     */
    public static function getCurrentLoginUserId()
    {
        return self::getCurrentTokenVar('uid');
    }

    /**
     * 获取当前登录的用户信息
     * @return UserModel|array|false|null
     * @throws BaseException
     */
    private function getLoginUser()
    {
        $userId = self::getCurrentLoginUserId();
        // 获取用户信息
        if (!$userInfo = UserModel::detail($userId)) {
            $this->error = '没有找到用户信息';
            return false;
        }

        return $userInfo;
    }


    /**
     * 根据关键词获取当前token值
     */
    protected static function getCurrentTokenVar($key){

        $token = Request::instance()->header("Access-Token");

        $vars = Cache::get($token);
        if(!$vars){
            return throwError('Token已过期或无效Token');
        }else{
            if(!is_array($vars)){
                $vars = json_decode($vars, true);
            }
            if(array_key_exists($key, $vars)){
                return $vars[$key];
            }else{
                return throwError('尝试获取的Token变量并不存在');
            }
        }
    }

}